package com.kkm.dao.Imp;

import com.kkm.dao.UserDao;
import com.kkm.pojo.Consumer;
import com.kkm.util.JdbcUtils_Druid;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;

import java.util.*;

/**
 * User管理dao层接口实现类
 */
public class UserDaoImp implements UserDao {
    private JdbcTemplate template=new JdbcTemplate(JdbcUtils_Druid.getDataSource());

    @Override
    public List<Consumer> showUser(int index, int rows) {
        String sql="select * from consumer limit ?,?";
        return template.query(sql,new BeanPropertyRowMapper<>(Consumer.class),index,rows);
    }

    @Override
    public int checkUserCount() {
        String sql="select count(*) from consumer ";
        return template.queryForObject(sql,int.class);
    }

    @Override
    public int checkUserNum(Map<String, String[]> condition) {
        try{
        String sql="select count(*) from consumer where 1=1 ";
        //解析map  1.对sql拼接
        StringBuffer sb=new StringBuffer(sql);
        //2.获取键值，name address，email
        Set<String> keySet=condition.keySet();
        //3.定义参数集合
        List<Object> params=new ArrayList<Object>();
        //遍历看那个不为空拼接到sql语句中
        //key：name，address，email
        for(String key:keySet){
            //排除分页条件参数
            if ("currentPage".equals(key)||"rows".equals(key)||"method".equals(key)){
                continue;
            }
            String value=condition.get(key)[0];//获取值
            if(value!=null||"".equals(value)){
                //有值
                sb.append(" and "+key+" like ? ");
                params.add("%%"+value+"%");
            }
        }
        return template.queryForObject(sb.toString(),int.class,params.toArray());
        }catch (Exception e){
            return 0;
        }
    }

    @Override
    public void addUser(String tname) {

    }

    @Override
    public List<Consumer> checkUser(Map<String, String[]> condition, int index, int rows) {
        try{
        String sql="select * from consumer where 1=1 ";
        //解析map  1.对sql拼接
        StringBuffer sb=new StringBuffer(sql);
        //2.获取键值，name address，email
        Set<String> keySet=condition.keySet();
        //3.定义参数集合
        List<Object> params=new ArrayList<Object>();
        //遍历看那个不为空拼接到sql语句中
        //key：name，address，email
        for(String key:keySet){
            //排除分页条件参数
            if ("currentPage".equals(key)||"rows".equals(key)||"method".equals(key)){
                continue;
            }
            String value=condition.get(key)[0];//获取值
            if(value!=null||"".equals(value)){
                //有值
                sb.append(" and "+key+" like ? ");
                params.add("%%"+value+"%");
            }
        }
        sb.append(" limit ?,? ");
        params.add(index);
        params.add(rows);
        sql=sb.toString();
        return template.query(sql,new BeanPropertyRowMapper<Consumer>(Consumer.class),params.toArray());
        }catch (Exception e){
            return null;
        }
    }

    @Override
    public void deleteUser(int uid) {
        String  sql="delete from consumer where id=? ";
        template.update(sql,uid);
    }

    @Override
    public Consumer userById(int uid) {
        String sql="select * from consumer where id=?";
        return template.queryForObject(sql,new BeanPropertyRowMapper<Consumer>(Consumer.class),uid);
    }

    @Override
    public void modifyUser(int uid, String username, int sex, Date birth, String phone, String email, String address) {
        String sql="update consumer set username=?,sex=?,phone_num=?,email=?,birth=?,location=? where id=?";
        template.update(sql,username,sex,phone,email,birth,address,uid);
    }
}
